package condition;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Condition 配合Lock  实现线程的等待 与通知
 */
public class ConditionTest {
  public static ReentrantLock lock = new ReentrantLock();
  public static Condition condition = lock.newCondition();

  public static void main(String[] args) {
    new Thread() {
      @Override
      public void run() {
        lock.lock();//请求锁
        System.out.println(Thread.currentThread().getName() + "==》加锁");
        try {
          System.out.println(Thread.currentThread().getName() + "==》进入等待");
          //使当前线程加入 await() 等待队列中，并释放当锁，当其他线程调用signal()会重新请求锁。与Object.wait()类似。
          condition.await();//设置当前线程进入等待
          System.out.println(Thread.currentThread().getName() + "==》执行剩下的");
        } catch (InterruptedException e) {
          e.printStackTrace();
        } finally {
          System.out.println(Thread.currentThread().getName() + "释放锁");
          lock.unlock();//释放锁
        }
        System.out.println(Thread.currentThread().getName() + "==》继续执行");
      }
    }.start();
    new Thread() {
      @Override
      public void run() {
        lock.lock();//请求锁
        System.out.println(Thread.currentThread().getName() + "=》加锁");
        try {
          System.out.println(Thread.currentThread().getName() + "=》进入");
          Thread.sleep(2000);//休息2秒
          condition.signal();//随机唤醒等待队列中的一个线程
          System.out.println(Thread.currentThread().getName() + "休息结束");
        } catch (InterruptedException e) {
          e.printStackTrace();
        } finally {
          System.out.println(Thread.currentThread().getName() + "释放锁");
          lock.unlock();//释放锁
        }
      }
    }.start();
  }
}